package com.chosen.mytwitter.provider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

import com.example.DbHelper;

public class TimelineDBProvider extends ContentProvider {
	static final String TAG = "TimelineDBProvider";
	private static final int LISTS = 1;
	private static final int LIST_ID = 2;
	private static final int TIMELINE = 3;
	private static final int LIST_TIMELINE = 4;
	public static final Uri CONTENT_URI = Uri
			.parse("content://mytwitter.TimelineCP");
	private static final String LIST_TABLE = "lists";
	private static final String TIMELINE_TABLE = "timeline";
	private static final String LIST_TIMELINE_TABLE = "timeline t INNER JOIN lists l on t.user=l.user";
	private DbHelper dbHelper;
	private static final String PROVIDER_NAME = "mytwitter.TimelineCP";
	private SQLiteDatabase db;
	private static final UriMatcher uriMatcher;
	static {
		uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		uriMatcher.addURI(PROVIDER_NAME, "lists", LISTS);
		uriMatcher.addURI(PROVIDER_NAME, "lists/*", LIST_ID);
		uriMatcher.addURI(PROVIDER_NAME, "timeline", TIMELINE);
		uriMatcher.addURI(PROVIDER_NAME, "listtimeline", LIST_TIMELINE);
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		int count = 0;
		switch (uriMatcher.match(uri)) {
		case LISTS:
			count = db.delete(LIST_TABLE, selection, null);
			Log.d(TAG, Integer.toString(count));
			break;
		case LIST_ID:
			String id = uri.getPathSegments().get(1);
			count = db.delete(LIST_TABLE, "_id = '"
					+ id
					+ "'"
					+ (!TextUtils.isEmpty(selection) ? " AND (" + selection
							+ ')' : ""), selectionArgs);
			break;
		default:
			Log.d(TAG, "Could not match URI");
		}
		return count;
	}

	@Override
	public String getType(Uri paramUri) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Uri insert(Uri paramUri, ContentValues paramContentValues) {
		Uri _uri = null;
		long rowID = 0;
		String table = null;
		switch (uriMatcher.match(paramUri)) {
		case TIMELINE:
			table = TIMELINE_TABLE;
			break;
		case LISTS:
			table = LIST_TABLE;
			break;
		default:
			throw new IllegalArgumentException("No matching URI was found");
		}
		try {
			rowID = db.insertOrThrow(table, null, paramContentValues);

			if (rowID > 0) {
				_uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
				getContext().getContentResolver().notifyChange(_uri, null);
			}
			// We have a new status
		} catch (SQLException e) {
		}
		return _uri;

	}

	@Override
	public boolean onCreate() {
		dbHelper = new DbHelper(getContext());
		db = dbHelper.getReadableDatabase();
		return (dbHelper == null) ? false : true;
	}

	@Override
	public Cursor query(Uri paramUri, String[] projection, String selection,
			String[] selectionArray, String sortOrder) {
		String table = null;
		switch (uriMatcher.match(paramUri)) {
		case TIMELINE:
			table = TIMELINE_TABLE;
			break;
		case LISTS:
			table = LIST_TABLE;
			break;
		case LIST_TIMELINE:
			table = LIST_TIMELINE_TABLE;
			break;
		default:
			throw new IllegalArgumentException("No matching URI was found");
		}
		SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		qb.setTables(table);
		Cursor c = qb.query(db, projection, selection, selectionArray, null,
				null, sortOrder);
		c.setNotificationUri(getContext().getContentResolver(), paramUri);
		return c;
	}

	@Override
	public int update(Uri paramUri, ContentValues paramContentValues,
			String paramString, String[] paramArrayOfString) {
		// TODO Auto-generated method stub
		return 0;
	}

}
